import scrapy

class QidianSpider(scrapy.Spider):
    name = "qidian"

    async def start(self):
        # 先测试是否能正常访问首页
        yield scrapy.Request(
            'https://www.qidian.com/',
            callback=self.test_connection,
            headers={
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
            }
        )

    def test_connection(self, response):
        print(f"首页状态码: {response.status}")
        print(f"首页标题: {response.css('title::text').get()}")

        # 检查页面内容
        if "起点" in response.text:
            print("成功访问起点首页")
            # 如果首页能访问，再尝试具体页面
            yield scrapy.Request(
                'https://www.qidian.com/finish/vip0-/',
                callback=self.parse,
                headers={
                    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
                }
            )
        else:
            print("可能被重定向或限制访问")

    def parse(self, response):
        print(f"具体页面状态码: {response.status}")
        # ... 原有的解析逻辑